<template>
  <div class="trend-chart">
    <div ref="chartContainer" class="chart-container"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'TrendChart',
  props: {
    chartData: {
      type: Object,
      default: () => ({})
    },
    chartType: {
      type: String,
      default: 'sales' // sales or visit
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart();
  },
  beforeUnmount() {
    if (this.chart) {
      this.chart.dispose();
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.chartContainer);
      this.updateChart();
    },
    updateChart() {
      if (!this.chart) return;
      
      const title = this.chartType === 'sales' ? '销售额趋势' : '访问量趋势';
      const data = this.chartType === 'sales' ? this.chartData.salesData : this.chartData.visitData;
      
      const option = {
        title: {
          text: title,
          left: 'center'
        },
        tooltip: {
          trigger: 'axis'
        },
        xAxis: {
          type: 'category',
          data: this.chartData.times || []
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: data || [],
          type: 'line',
          smooth: true,
          itemStyle: {
            color: '#3a68f2'
          },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
              offset: 0,
              color: 'rgba(58, 104, 242, 0.3)'
            }, {
              offset: 1,
              color: 'rgba(58, 104, 242, 0.05)'
            }])
          }
        }]
      };
      
      this.chart.setOption(option);
    }
  },
  watch: {
    chartData: {
      handler() {
        this.updateChart();
      },
      deep: true
    },
    chartType: {
      handler() {
        this.updateChart();
      }
    }
  }
}
</script>

<style scoped>
.trend-chart {
  width: 100%;
  height: 100%;
}

.chart-container {
  width: 100%;
  height: 400px;
}
</style>